home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / program / 441 / dlibs12 / prtfld.c < prev    next >
Text File  |  1990-11-23  |  1KB  |  68 lines

  1. _prtfld(op, put, buf, ljustf, sign, pad, width, preci)
  2.     register char *op;
  3.     register int (*put)();
  4.     register unsigned char *buf;
  5.     int ljustf;
  6.     register char sign;
  7.     char pad;
  8.     register int width;
  9.     int preci;
  10. /*
  11.  *    Output the given field in the manner specified by the arguments.
  12.  *    Return the number of characters output.
  13.  */
  14.     {
  15.     register int cnt = 0, len;
  16.     register unsigned char ch;
  17.  
  18.     len = strlen(buf);
  19.  
  20.     if (*buf == '-')
  21.         sign = *buf++;
  22.     else if (sign)
  23.         len++;
  24.  
  25.     if ((preci != -1) && (len > preci))    /* limit max data width */
  26.         len = preci;
  27.  
  28.     if (width < len)    /* flexible field width or width overflow */
  29.         width = len;
  30.  
  31. /* at this point:
  32.  *    width = total field width
  33.  *    len   = actual data width (including possible sign character)
  34.  */
  35.     cnt = width;
  36.     width -= len;
  37.  
  38.     while (width || len)
  39.         {
  40.         if (!ljustf && width)        /* left padding */
  41.             {
  42.             if (len && sign && (pad == '0'))
  43.                 goto showsign;
  44.             ch = pad;
  45.             --width;
  46.             }
  47.         else if (len)
  48.             {
  49.             if (sign)
  50.                 {
  51. showsign:            ch = sign;    /* sign */
  52.                 sign = '\0';
  53.                 }
  54.             else
  55.                 ch = *buf++;    /* main field */
  56.             --len;
  57.             }
  58.         else
  59.             {
  60.             ch = pad;        /* right padding */
  61.             --width;
  62.             }
  63.         (*put)(ch, op);
  64.         }
  65.  
  66.     return(cnt);
  67.     }
  68.